<?php echo $header; ?>
<?php if($_GET['click']){?>
<script type="text/javascript"><!--
$(document).ready(function() {
  $('#tabs-<?php echo $_GET['click'] ;?>').click();
});
//--></script> 
<?php }?>
<div id="content">
  <div class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <?php echo $breadcrumb['separator']; ?><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
    <?php } ?>
  </div>
  <?php if ($error_warning) { ?>
  <div class="warning"><?php echo $error_warning; ?></div>
  <?php } ?>
  <div class="box">
    <div class="heading">
      <h1><img src="view/image/category.png" alt="" /> <?php echo $heading_title; ?></h1>
      <div class="buttons"><a onclick="$('#form').submit();" class="button"><?php echo $button_save; ?></a><a onclick="location = '<?php echo $cancel; ?>';" class="button"><?php echo $button_cancel; ?></a></div>
    </div>
    <div class="content">
      <div id="tabs" class="htabs"><a href="#tab-general"><?php echo $tab_general; ?></a><a href="#tab-data"><?php echo $tab_data; ?></a><a id="tabs-attribute" href="#tab-attribute">Attribute</a><!-- <a href="#tab-design"><?php echo $tab_design; ?></a> --></div>
      <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form">
        <div id="tab-general">
          <div id="languages" class="htabs">
            <?php foreach ($languages as $language) { ?>
            <a href="#language<?php echo $language['language_id']; ?>"><img src="view/image/flags/<?php echo $language['image']; ?>" title="<?php echo $language['name']; ?>" /> <?php echo $language['name']; ?></a>
            <?php } ?>
          </div>
          <?php foreach ($languages as $language) { ?>
          <div id="language<?php echo $language['language_id']; ?>">
            <table class="form">
              <tr>
                <td><span class="required">*</span> <?php echo $entry_name; ?></td>
                <td><input type="text" name="category_description[<?php echo $language['language_id']; ?>][name]" size="100" value="<?php echo isset($category_description[$language['language_id']]) ? $category_description[$language['language_id']]['name'] : ''; ?>" />
                  <?php if (isset($error_name[$language['language_id']])) { ?>
                  <span class="error"><?php echo $error_name[$language['language_id']]; ?></span>
                  <?php } ?></td>
              </tr>
              <tr>
                <td><?php echo $entry_meta_description; ?></td>
                <td><textarea name="category_description[<?php echo $language['language_id']; ?>][meta_description]" cols="40" rows="5"><?php echo isset($category_description[$language['language_id']]) ? $category_description[$language['language_id']]['meta_description'] : ''; ?></textarea></td>
              </tr>
              <tr>
                <td><?php echo $entry_meta_keyword; ?></td>
                <td><textarea name="category_description[<?php echo $language['language_id']; ?>][meta_keyword]" cols="40" rows="5"><?php echo isset($category_description[$language['language_id']]) ? $category_description[$language['language_id']]['meta_keyword'] : ''; ?></textarea></td>
              </tr>
              <tr>
                <td><?php echo $entry_description; ?></td>
                <td><textarea name="category_description[<?php echo $language['language_id']; ?>][description]" id="description<?php echo $language['language_id']; ?>"><?php echo isset($category_description[$language['language_id']]) ? $category_description[$language['language_id']]['description'] : ''; ?></textarea></td>
              </tr>
            </table>
          </div>
          <?php } ?>
        </div>
        <div id="tab-data">
          <table class="form">
            <tr>
              <td><?php echo $entry_parent; ?></td>
              <td><select name="parent_id">
                  <option value="0"><?php echo $text_none; ?></option>
                  <?php foreach ($categories as $category) { ?>
                  <?php if ($category['category_id'] == $parent_id) { ?>
                  <option value="<?php echo $category['category_id']; ?>" selected="selected"><?php echo $category['name']; ?></option>
                  <?php } else { ?>
                  <option value="<?php echo $category['category_id']; ?>"><?php echo $category['name']; ?></option>
                  <?php } ?>
                  <?php } ?>
                </select></td>
            </tr>
           <!--  <?php if($route!="catalog/category/insert"){?>
            <tr>
              <td></td>
              <td>
                <table class="form" id="attribute_table">
                  <tr>
                    <td>Attibute</td>
                    <td><?php echo $is_required ;?></td>
                    <td><?php echo $is_screen ;?></td>
                    <td><?php echo $is_default ;?></td>
                    <td>Order</td>
                    <td><?php echo $op ; ?></td>
                  </tr>
                  <tr>
                    <td>
                      <select id="product_cat">
                      <?php foreach ($attributes as $categorie) { ?>
                         <option value="<?php echo $categorie['attribute_id']; ?>"><?php echo $categorie['name']; ?></option>
                      <?php } ?>
                          </select>
                    </td>
                    <td><input type="checkbox" value=1 id="is_required" ></td>
                    <td><input type="checkbox" value=1 id="is_screen"></td>
                    <td><input type="checkbox" value=1 id="is_default"></td>
                    <td><input type="input" value=0 size=2 id="order" /></td>
                    <td><a href="javascript:void(0)" onclick="add()"><?php echo $add ;?></a></td>
                  </tr>
                  <?php if($select_cat){ ?>
                    <?php foreach($select_cat as $cat){ ?>
                    <tr id="cat_<?php echo $cat['attribute_id'];?>">
                      <td>
                        <input name="attribute[]" type="hidden" value="<?php echo $cat['attribute_id'] ?>" />
                        <span >
                          <?php echo $cat['name'];?>
                        </span>
                      </td>
                      <td><input type="checkbox" value=1 name="is_required_<?php echo $cat['attribute_id'];?>" <?php if($cat['is_required']==1) echo "checked";?> ></td>
                      <td><input type="checkbox" value=1 name="is_screen_<?php echo $cat['attribute_id'];?>" <?php if($cat['is_screen']==1) echo "checked";?> ></td>
                      <td><input type="checkbox" value=1 name="is_default_<?php echo $cat['attribute_id'];?>" <?php if($cat['is_default']==1) echo "checked";?> ></td>
                      <td><input type="text" name="cat_order_<?php echo $cat['attribute_id'];?>" size=2 value="<?php echo $cat['sort_order'];?>"></td>
                      <td><a href="javascript:void(0)" onclick="del(<?php echo $cat['attribute_id'];?>)"><?php echo $del ;?></a></td>
                    </tr>
                    <?php } ?>
                  <?php } ?>
                </table>
              </td>
            </tr>
            <?php }?> -->

            <tr>
              <td><?php echo $entry_store; ?></td>
              <td><div class="scrollbox">
                  <?php $class = 'even'; ?>
                  <div class="<?php echo $class; ?>">
                    <?php if (in_array(0, $category_store)) { ?>
                    <input type="checkbox" name="category_store[]" value="0" checked="checked" />
                    <?php echo $text_default; ?>
                    <?php } else { ?>
                    <input type="checkbox" name="category_store[]" value="0" />
                    <?php echo $text_default; ?>
                    <?php } ?>
                  </div>
                  <?php foreach ($stores as $store) { ?>
                  <?php $class = ($class == 'even' ? 'odd' : 'even'); ?>
                  <div class="<?php echo $class; ?>">
                    <?php if (in_array($store['store_id'], $category_store)) { ?>
                    <input type="checkbox" name="category_store[]" value="<?php echo $store['store_id']; ?>" checked="checked" />
                    <?php echo $store['name']; ?>
                    <?php } else { ?>
                    <input type="checkbox" name="category_store[]" value="<?php echo $store['store_id']; ?>" />
                    <?php echo $store['name']; ?>
                    <?php } ?>
                  </div>
                  <?php } ?>
                </div></td>
            </tr>
            <tr>
              <td><?php echo $entry_keyword; ?></td>
              <td><input type="text" name="keyword" value="<?php echo $keyword; ?>" /></td>
            </tr>
            <tr>
              <td><?php echo $entry_image; ?></td>
              <td valign="top"><div class="image"><img src="<?php echo $thumb; ?>" alt="" id="thumb" />
                <input type="hidden" name="image" value="<?php echo $image; ?>" id="image" />
                <br /><a onclick="image_upload('image', 'thumb');"><?php echo $text_browse; ?></a>&nbsp;&nbsp;|&nbsp;&nbsp;<a onclick="$('#thumb').attr('src', '<?php echo $no_image; ?>'); $('#image').attr('value', '');"><?php echo $text_clear; ?></a></div></td>
            </tr>
            <tr>
              <td><?php echo $entry_top; ?></td>
              <td><?php if ($top) { ?>
                <input type="checkbox" name="top" value="1" checked="checked" />
                <?php } else { ?>
                <input type="checkbox" name="top" value="1" />
                <?php } ?></td>
            </tr>
            <tr>
              <td><?php echo $entry_column; ?></td>
              <td><input type="text" name="column" value="<?php echo $column; ?>" size="1" /></td>
            </tr>
            <tr>
              <td><?php echo $entry_sort_order; ?></td>
              <td><input type="text" name="sort_order" value="<?php echo $sort_order; ?>" size="1" /></td>
            </tr>
            <tr>
              <td><?php echo $entry_status; ?></td>
              <td><select name="status">
                  <?php if ($status) { ?>
                  <option value="1" selected="selected"><?php echo $text_enabled; ?></option>
                  <option value="0"><?php echo $text_disabled; ?></option>
                  <?php } else { ?>
                  <option value="1"><?php echo $text_enabled; ?></option>
                  <option value="0" selected="selected"><?php echo $text_disabled; ?></option>
                  <?php } ?>
                </select></td>
            </tr>
          </table>
        </div>
        <div id="tab-attribute">
          <table class="form">
            <tr>
              <td class="right">Name:<input type="text" id="group_name">Order:<input type="text" size=3 id="group_order"><a onclick="add_attr_group();" class="button">Add AttributeGroup</a></td>
            </tr>
            <tr>
              <td class="right">Name:<input type="text" id="name">Order:<input type="text" size=3 id="order">
              <select id="category_all">
                <?php if ($all_attributes) {
                  foreach ($all_attributes as $key => $value) {?>
                    <option value="<?php echo $value['attribute_group_id'];?>"><?php echo $value['group_name'];?></option>
                  <?php }
                }?>
              </select>
                <a onclick="add_attr();" class="button">Add Attribute</a></td>
            </tr>

            <?php if($attributes){?>
              <table class="list" style="width:70%">
                    <thead>
                    <tr>
                      <td class="left" style="width:60%">Name</td>
                      <td class="left" width="10%">Sort Order</td>
                      <td width="10%">is Screen</td>
                      <td width="10%">is Default</td>
                      <td width="10%">op</td>
                    </tr>
                    </thead>
              </table>
              <?php for ($i=0; $i < count($attributes); $i++) {
                if($i==0||($i!=0&&$attributes[$i]['attribute_group_id']!=$attributes[$i-1]['attribute_group_id'])){ ?>
                  <table class="list" style="width:70%">
                    <thead>
                    <tr>
                      <td class="left" style="width:60%"><input type="text" name="attr_group_name_<?php echo $attributes[$i]['attribute_group_id']?>" value="<?php echo $attributes[$i]['group_name'];?>"></td>
                      <td class="left" width="10%"><input type="text" size=3 name="group_order_<?php echo $attributes[$i]['attribute_group_id']?>" value="<?php echo $attributes[$i]['order'];?>"></td>
                      <td width="10%"></td>
                      <td width="10%"></td>
                      <td width="10%"><?php 
                if (0==$attributes[$i]['attribute_id']){ ?><a href="javascript:void(0)" onclick="del_attr_group(<?php echo $attributes[$i]['attribute_group_id'];?>)"><img src="view/image/hot0.png"></a><?php } ?></td>
                    </tr>
                    </thead>
                <?php }
                if (0!=$attributes[$i]['attribute_id']){ ?>
                  <tr>
                    <td class="left" ><input style="margin-left:20px;"  type="text" name="attr_name_<?php echo $attributes[$i]['attribute_id']?>" value="<?php echo $attributes[$i]['name'];?>"></td>
                    <td class="left"><input type="text" size=3 name="order_<?php echo $attributes[$i]['attribute_id']?>" value="<?php echo $attributes[$i]['sort_order'];?>"></td>
                    <td width="10%"><input type="checkbox" name="is_screen_<?php echo $attributes[$i]['attribute_id']?>" value="1" <?php if($attributes[$i]['is_screen']){?>checked <?php }?> ></td>
                    <td width="10%"><input type="checkbox" name="is_default_<?php echo $attributes[$i]['attribute_id']?>" value="1" <?php if($attributes[$i]['is_default']){?>checked <?php }?> ></td>
                    <td width="10%"><a href="javascript:void(0)" onclick="del_attr(<?php echo $attributes[$i]['attribute_id']?>)"><img src="view/image/hot0.png"></a></td>
                  </tr>
                  <?php }
                if ($attributes[$i]['attribute_group_id']!=$attributes[$i+1]['attribute_group_id']) { ?>
                  </table>
                <?php }
              } 
            }?>
          </table>
        </div>
        <!-- <div id="tab-design">
          <table class="list">
            <thead>
              <tr>
                <td class="left"><?php echo $entry_store; ?></td>
                <td class="left"><?php echo $entry_layout; ?></td>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td class="left"><?php echo $text_default; ?></td>
                <td class="left"><select name="category_layout[0][layout_id]">
                    <option value=""></option>
                    <?php foreach ($layouts as $layout) { ?>
                    <?php if (isset($category_layout[0]) && $category_layout[0] == $layout['layout_id']) { ?>
                    <option value="<?php echo $layout['layout_id']; ?>" selected="selected"><?php echo $layout['name']; ?></option>
                    <?php } else { ?>
                    <option value="<?php echo $layout['layout_id']; ?>"><?php echo $layout['name']; ?></option>
                    <?php } ?>
                    <?php } ?>
                  </select></td>
              </tr>
            </tbody>
            <?php foreach ($stores as $store) { ?>
            <tbody>
              <tr>
                <td class="left"><?php echo $store['name']; ?></td>
                <td class="left"><select name="category_layout[<?php echo $store['store_id']; ?>][layout_id]">
                    <option value=""></option>
                    <?php foreach ($layouts as $layout) { ?>
                    <?php if (isset($category_layout[$store['store_id']]) && $category_layout[$store['store_id']] == $layout['layout_id']) { ?>
                    <option value="<?php echo $layout['layout_id']; ?>" selected="selected"><?php echo $layout['name']; ?></option>
                    <?php } else { ?>
                    <option value="<?php echo $layout['layout_id']; ?>"><?php echo $layout['name']; ?></option>
                    <?php } ?>
                    <?php } ?>
                  </select></td>
              </tr>
            </tbody>
            <?php } ?>
          </table>
        </div> -->
      </form>
    </div>
  </div>
</div>
<script type="text/javascript" src="view/javascript/ckeditor/ckeditor.js"></script> 
<script type="text/javascript"><!--
<?php foreach ($languages as $language) { ?>
CKEDITOR.replace('description<?php echo $language['language_id']; ?>', {
	filebrowserBrowseUrl: 'index.php?route=common/filemanager&token=<?php echo $token; ?>',
	filebrowserImageBrowseUrl: 'index.php?route=common/filemanager&token=<?php echo $token; ?>',
	filebrowserFlashBrowseUrl: 'index.php?route=common/filemanager&token=<?php echo $token; ?>',
	filebrowserUploadUrl: 'index.php?route=common/filemanager&token=<?php echo $token; ?>',
	filebrowserImageUploadUrl: 'index.php?route=common/filemanager&token=<?php echo $token; ?>',
	filebrowserFlashUploadUrl: 'index.php?route=common/filemanager&token=<?php echo $token; ?>'
});
<?php } ?>
//--></script> 
<script type="text/javascript"><!--
function image_upload(field, thumb) {
	$('#dialog').remove();
	
	$('#content').prepend('<div id="dialog" style="padding: 3px 0px 0px 0px;"><iframe src="index.php?route=common/filemanager&token=<?php echo $token; ?>&field=' + encodeURIComponent(field) + '" style="padding:0; margin: 0; display: block; width: 100%; height: 100%;" frameborder="no" scrolling="auto"></iframe></div>');
	
	$('#dialog').dialog({
		title: '<?php echo $text_image_manager; ?>',
		close: function (event, ui) {
			if ($('#' + field).attr('value')) {
				$.ajax({
					url: 'index.php?route=common/filemanager/image&token=<?php echo $token; ?>&image=' + encodeURIComponent($('#' + field).val()),
					dataType: 'text',
					success: function(data) {
						$('#' + thumb).replaceWith('<img src="' + data + '" alt="" id="' + thumb + '" />');
					}
				});
			}
		},	
		bgiframe: false,
		width: 800,
		height: 400,
		resizable: false,
		modal: false
	});
};
//--></script> 
<script type="text/javascript"><!--
  function add(){
    attribute_id=$("#product_cat").val();
    is_required=$("#is_required").attr('checked')==undefined?0:1;
    is_default=$("#is_default").attr('checked')==undefined?0:1;
    is_screen=$("#is_screen").attr('checked')==undefined?0:1;
    cat_order=parseInt($("#order").val());
    cat_order=isNaN(cat_order)?0:cat_order;

    $.ajax({
      url: 'index.php?route=catalog/attribute/add&token=<?php echo $token; ?>',
      data:{"id":attribute_id,"product_cat":<?php echo $category_id;?>,"is_required":is_required,"is_default":is_default,"is_screen":is_screen,"order":cat_order},
      type:"POST",
      success: function(json) {
        if(json==1){
          str="<tr id='cat_"+attribute_id+"'><td><input type='hidden' value="+attribute_id+" name='attribute[]''>"+$('#product_cat option:selected').text()+'</td><td><input type="checkbox" value=1 name="is_required_'+attribute_id+'"';
          if(is_required){
            str+=" checked ";
          }
          str+='><td><input type="checkbox" value=1 name="is_screen_'+attribute_id+'"';
          if(is_screen){
            str+=" checked ";
          }
          str+='></td>  <td><input type="checkbox" value=1 name="is_default_'+attribute_id+'"';
          if(is_default){
            str+=" checked ";
          }
          str+='</td><td><input type="text" size=2 name="cat_order_'+attribute_id+'" value="'+cat_order+'"></td><td><a href="javascript:void(0)" onclick="del('+attribute_id+')"><?php echo $del ;?></a></td></tr>';
          $("#attribute_table").append(str);
        }else{
          alert("Failer!");
        }
      }
    }); 
  }



  /**
    add attr
  */
  function add_attr(){
    $.ajax({
        url: 'index.php?route=catalog/attribute/add_attr&token=<?php echo $token; ?>',
        data:{"category_id":<?php echo $category_id;?>,"name":$('#name').val(),"order":$('#order').val(),"attribute_group_id":$('#category_all').val()},
        type:"POST",
        success: function(json) {
          if(json==1){
            alert("success");
            location=location+"&click=attribute";
          }else{
            alert("Failer!");
          }
        }
      });
  }

  function del_attr(id){
    if(confirm("DEL?")){
      $.ajax({
          url: 'index.php?route=catalog/attribute/del_attr&token=<?php echo $token; ?>',
          data:{"attribute_id":id},
          type:"POST",
          success: function(json) {
            if(json==1){
              alert("success");
              location=location+"&click=attribute";
            }else{
              alert("Failer!");
            }
          }
        });
    }
  }

  function add_attr_group(){
    $.ajax({
        url: 'index.php?route=catalog/attribute/add_attr_group&token=<?php echo $token; ?>',
        data:{"category_id":<?php echo $category_id;?>,"name":$('#group_name').val(),"order":$('#group_order').val()},
        type:"POST",
        success: function(json) {
          if(json==1){
            alert("success");
            location=location+"&click=attribute";
          }else{
            alert("Failer!");
          }
        }
      });
  }
  function del_attr_group(id){
    if(confirm("DEL?")){
      $.ajax({
          url: 'index.php?route=catalog/attribute/del_attr_group&token=<?php echo $token; ?>',
          data:{"attribute_group_id":id},
          type:"POST",
          success: function(json) {
            if(json==1){
              alert("success");
              location=location+"&click=attribute";
            }else{
              alert("Failer!");
            }
          }
        });
    }
  }
  function del(id){
    if(confirm("DEL?")){
      $.ajax({
        url: 'index.php?route=catalog/attribute/del&token=<?php echo $token; ?>',
        data:{"product_cat":<?php echo $category_id;?>,"id":id},
        success: function(json) {
          if(json==1){
            $("#cat_"+id).remove();
          }else{
            alert("Failer!");
          }
        }
      });
    }
  }

//--></script> 
<script type="text/javascript"><!--
$('#tabs a').tabs(); 
$('#languages a').tabs();
//--></script> 
<?php echo $footer; ?>